package com.egreen.user.service.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.pojo.VO.user.AgentApplicationVO;
import com.egreen.common.pojo.entity.user.AgentApplication;
import com.egreen.common.pojo.entity.user.User;
import com.egreen.common.pojo.entity.user.UserLoginAccount;
import com.egreen.common.serviceapi.user.IAgentServiceApi;
import com.egreen.user.dao.UserLoginAccountDao;
import com.egreen.user.service.IAgentApplicationService;
import com.egreen.user.service.IAgentAreaService;
import com.egreen.user.service.IUserLoginAccountService;
import com.egreen.user.service.IUserService;
import com.egreen.user.utils.UserUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cloudJiang
 * @date 2022/9/22 14:05
 */
@Component
@DubboService
public class AgentServiceApi implements IAgentServiceApi {
    @Autowired
    IUserService userService;
    @Autowired
    IAgentApplicationService agentApplicationService;
    @Autowired
    IAgentAreaService agentAreaService;
    @Autowired
    IUserLoginAccountService loginAccountService;
    @Autowired
    UserUtil util;


    @Override
    public List<AgentApplicationVO> getAgentApplicationList(Integer state) {
        List<AgentApplicationVO> returnList = new ArrayList<>();
        //根据是否传入state来选择是否拼接条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(state != null, "state", state);

        //查找申请表
        List<AgentApplication> agentList = agentApplicationService.getBaseMapper().selectList(wrapper);
        for (AgentApplication agentApplication : agentList) {
            //开始拼接Vo
            AgentApplicationVO agentApplicationVO = new AgentApplicationVO();
            agentApplicationVO.setId(agentApplication.getId());
            agentApplicationVO.setState(agentApplication.getState());
            agentApplicationVO.setLevel(agentApplication.getLevel());
            agentApplicationVO.setUserId(agentApplication.getUserId());
            agentApplicationVO.setArea(agentAreaService.getById(agentApplication.getId()).getArea());

            returnList.add(agentApplicationVO);
        }

        return returnList;
    }

    @Override
    public void refuseAgentApplication(Long id) {
        //首先更改申请表状态
        String applicationState = "未通过";
        AgentApplication agentApplication = agentApplicationService.getById(id);
        agentApplication.setState(applicationState);
        agentApplicationService.updateById(agentApplication);
        // 之后获取用户邮箱发送邮件通知
        Long userId = agentApplication.getUserId();
        Long loginAccountId = userService.getById(userId).getLoginAccountId();
        UserLoginAccount loginAccount = loginAccountService.getById(loginAccountId);
        String userEmail = loginAccount.getEmail();
        //发送邮件
        util.sendNoticeEmail(userEmail, applicationState);
    }

    @Override
    public void passAgentApplication(Long id) {
        //首先更改申请表状态
        String applicationState = "已通过";
        AgentApplication agentApplication = agentApplicationService.getById(id);
        agentApplication.setState(applicationState);
        agentApplicationService.updateById(agentApplication);

        //更改用户状态
        User user = userService.getById(agentApplication.getUserId());
        String state = agentApplication.getState();
        switch (state) {
            case "社区回收员":
                user.setStates(1);
            case "区域回收员":
                user.setStates(2);
            case "城市回收员":
                user.setStates(3);
        }
        userService.updateById(user);
        // 之后获取用户邮箱发送邮件通知
        Long loginAccountId = user.getLoginAccountId();
        UserLoginAccount loginAccount = loginAccountService.getById(loginAccountId);
        String userEmail = loginAccount.getEmail();
        //发送邮件
        util.sendNoticeEmail(userEmail, applicationState);
    }
}
